package cn.edu.ctbu.demo1.service.impl;

import cn.edu.ctbu.demo1.domain.Admin;
import cn.edu.ctbu.demo1.repository.AdminRepository;
import cn.edu.ctbu.demo1.service.AdminService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class AdminlmpService implements AdminService {
    @Autowired //帮我new一个对象 满足adminRepository这个接口  IOC
    private AdminRepository adminRepository; //adminRepository接口，不能new
    public List<Admin> findAll(){
        return adminRepository.findAll();
    }

    /**
     * 只支持分页的查询
     * @param pageable
     * @return
     */
    public Page<Admin> findAll(Pageable pageable){
        return adminRepository.findAll(pageable);
    }
    public List<Admin> findAll(Example<Admin> example){
        return adminRepository.findAll(example);
    }
    /**
     *按id进行查询
     * @param id 主键，整数
     * @return
     */
    public Admin findById(Integer id){


        return  adminRepository.findById(id).orElse(null);
    }

//    /**
//     *按姓名和密码进行查询 ,like
//     * @param name
//     * @param password
//     * @return
//     */
//    public List<Admin> findByNameAndPassword(String name,String password){
//
//        return  adminRepository.findByNameAndPassword(name,password);
//    }
    public List<Admin> findByNoAndPassword(String no,String password){
        return adminRepository.findByNoAndPassword(no, password);
    }

    public Admin insert(Admin admin){
        adminRepository.save(admin);
        return admin;
    }
    public Admin update(Admin admin){
        adminRepository.save(admin);
        return admin;
    }

    public void delete(Integer id){
//        Admin admin= new Admin();
//        admin.setId(id);
        adminRepository.deleteById(id);
    }

    /**
     * 分页，带过滤的查询
     * @param admin
     * @param pageable
     * @return
     */
    public Page<Admin> findAll(Example<Admin> admin,Pageable pageable){
        return  adminRepository.findAll(admin,pageable);
    }
}
